
/*
选择图片
*/
function previewBoxClick(loadImgId) {
	$("#"+loadImgId).click();
}

/*
预览要上传的图片
*/
function loadImgChange(loadImgId, previewBoxId, previewImgId, selImgBt, upImgBts, width, height) {
	
	try {
		var result = checkPic(loadImgId); //浏览时间检测
		if(!result) {
			return;
		}
	} catch(e) {
		console.log("传图异常："+e);
	}
	
	setImagePreview(loadImgId, previewBoxId, previewImgId, width, height);
	$("#"+selImgBt).hide();
	$("#"+upImgBts).show();	
	//选择图片后直接上传
	$("#"+upImgBts).children("div")[0].click(); 
}

/*
删除图片
*/
function deleteImg(formId, previewImgId, selImgBt, upImgBts) {
	$("#"+previewImgId).hide();
	$("#"+previewImgId).prop("src", "");
	$("#"+selImgBt).show();
	$("#"+upImgBts).hide();
}

/**
 * 上传图片
 * 
 * @param formId
 * @param loadingPic	转动图DIV
 * @param upImgResult	上传结果DIV
 * @param upImgBts		上传按钮DIV
 * @param previewBoxId	预览DIV
 * @param imgUrl		图片路径存储
 */
function submitUpLoadImg(formId, loadingPic, upImgResult, upImgBts, previewBoxId, imgUrl) {
	$("#"+upImgBts).hide();
	$("#"+loadingPic).show();
	
	var imgCate = $("#"+formId).children(".imgCate").val();	//图片存储分类
	submitForm(formId,
		"../ueditor/jsp/controller.jsp?action=uploadimage&imgCate="+imgCate,
		function(data) {
			var resultJson = JSON.parse(data);
			var state = resultJson.state;
			var title = resultJson.title;
			var imageUrl = resultJson.url;
			if(state == "SUCCESS") {
				$("#"+imgUrl).val(imageUrl);	//上传成功后把返回的图片路径存放到#imgUrl中
				$("#"+upImgBts).hide();
				$("#"+loadingPic).hide();
				$("#"+upImgResult).show();
				//$("#"+previewBoxId).prop('onclick', false); //IE8不兼容“尚未实现”
				if(formId.indexOf('rate-pic-') >= 0) {	//放单：好评晒图使用
					$("#rate-pic-num").text(1 + parseInt($("#rate-pic-num").text()));
				}
			} else if(state == 88) {
				location.replace("login.html?redirectURL=" + window.location.href);
			} else {
				Utils.msgDialog(title+"["+ state +"]");
				$("#"+upImgBts).show();
				$("#"+loadingPic).hide();
			}
		}, function(XMLHttpRequest, textStatus, errorThrown) {
			Utils.msgDialog("图片上传失败，请重试！（图片过大或网速不给力，建议图片＜500K）"+textStatus+"[-99]");
			$("#"+upImgBts).show();
			$("#"+loadingPic).hide();
		}
	);
}

/*
ajax异步提交表单
	
@param formId
@param postUrl
@param {function} succFunc
@param {function} failFunc
*/
function submitForm(formId, postUrl, succFunc, failFunc) {
	$("#"+formId).ajaxSubmit({
		type : "POST",
        url : postUrl,
		timeout : 30000,
//		beforeSerialize : function() {
//			alert("表单数据序列化前执行的操作！");
//		},
//		beforeSubmit : function() {
//			alert("表单提交前执行的操作！");
//		},
		success : function(data) {
     		succFunc(data);
   		},
		error : function(data) {
			failFunc(data);
		}
	});
}
	

/*
图片上传预览

@param fileId			上传图片的INPUT
@param showImgDivId		显示预览图的外层DIV
@param previewImgId		显示预览图片的IMG
*/
function setImagePreview(fileId, showImgDivId, previewImgId, width, height) {
	
	var docObj = document.getElementById(fileId);
	var imgObjPreview = document.getElementById(previewImgId);
	
	try {
		if(docObj.files && docObj.files[0]) {
			//火狐下，直接设img属性
			imgObjPreview.style.display = 'block';
			imgObjPreview.style.width = width;
			imgObjPreview.style.height = height;                    
			//imgObjPreview.src = docObj.files[0].getAsDataURL();
	
			//火狐7以上版本不能用上面的getAsDataURL()方式获取，需要一下方式  
			imgObjPreview.src = window.URL.createObjectURL(docObj.files[0]);				
		} else {
			//IE下，使用滤镜
			docObj.select();
			
			var localImagId = document.getElementById(showImgDivId);
			
			var imgSrc = "";
			var version = window.navigator.userAgent;
			if(version.substr(version.indexOf("MSIE") + 5, 1) >= 9) {	//IE9以上版本
				imgSrc = docObj.value;
			    localImagId.focus();
			    imgSrc = document.selection.createRange().text;
			} else {
				imgSrc = document.selection.createRange().text;
			}
			
			//必须设置初始大小
			localImagId.style.width = width;
			localImagId.style.height = height;
			//图片异常的捕捉，防止用户修改后缀来伪造图片
			try {
				localImagId.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)";
				localImagId.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = imgSrc;
			} catch(e) {
				alert("您上传的图片格式不正确，请重新选择!");
				return false;
			}
			imgObjPreview.style.display = 'none';
			document.selection.empty();
		}
	} catch(e) {
		console.log("图片预览异常："+e);
		return false;
	}
	return true;
}

/**
 * 检测图片截图时间
 */
function checkPic(loadImgId) {
	var f = document.getElementById(loadImgId).files;
    //上次修改时间  
//    alert(f[0].lastModifiedDate);  
    //名称  
//    alert(f[0].name);  
    //大小 字节  
//    alert(f[0].size);  
    //类型  
//    alert(f[0].type);
	
	var mTime =  f[0].lastModifiedDate;
	var fSize = f[0].size;
	
	if(fSize > 2048000) {
		Utils.msgDialog("图片不能大于2M");
		return false;
	}
	
	var timestamp = (new Date(mTime)).valueOf();
	
	if(loadImgId.indexOf("doing-browseCenter") >= 0) { //中图
		ScreenshotTime.centerPicTime = timestamp;
		$.cookie("centerPicTime", timestamp);
	} else if(loadImgId.indexOf("doing-lookRate") >= 0) { //看评价图
		if(ScreenshotTime.centerPicTime == 0) {
			Utils.msgDialog("请先上传宝贝详情中部截图！");
			return false;
		}
		var temp0 = (timestamp - ScreenshotTime.centerPicTime) / 1000; //间隔秒数
		if(temp0 < 30) {
			Utils.msgDialog("截图时间间隔不能小于30秒，请在"+toTime(ScreenshotTime.centerPicTime + 30*1000)+"之后再截图！");
			return false;
		}
		ScreenshotTime.lookRatePicTime = timestamp;		
	} else if(loadImgId.indexOf("doing-lookSales") >= 0) { //看销量图
		if(ScreenshotTime.lookRatePicTime == 0) {
			Utils.msgDialog("请先上传查看评价截图！");
			return false;
		}
		var temp0 = (timestamp - ScreenshotTime.centerPicTime) / 1000; //间隔秒数
		if(temp0 < 60) {
			Utils.msgDialog("与中图时间间隔不能小于60秒，请在"+toTime(ScreenshotTime.centerPicTime + 60*1000)+"之后再截图！");
			return false;
		}
		var temp1 = (timestamp - ScreenshotTime.lookRatePicTime) / 1000;
		if(temp1 < 30) {
			Utils.msgDialog("截图时间间隔不能小于30秒，请在"+toTime(ScreenshotTime.lookRatePicTime + 30*1000)+"之后再截图！");
			return false;
		}
		ScreenshotTime.lookSalesPicTime = timestamp;		
	} else if(loadImgId.indexOf("doing-bottomPic") >= 0) { //底图
		var centerPicTime = parseInt($.cookie("centerPicTime"));
		var temp0 = (timestamp - centerPicTime) / 1000; //间隔秒数
		if(temp0 < 180) {
			alert("与中图时间间隔不能小于3分钟，请在"+toTime(centerPicTime + 180*1000)+"之后再截图！");
			return false;
		}
	}
	return true;
}

/**
 *  时间转换为标准格式 
 */
function toTime(obj) {
	var date = new Date(obj);
	var year = date.getFullYear()
	var month = date.getMonth() + 1
	var day = date.getDate();
	var h = date.getHours(); 
	var m = date.getMinutes(); 
	var s = date.getSeconds();
	month = (""+month).length == 1 ? "0"+month : month;
	day = (""+day).length == 1 ? "0"+day : day;
	h = (""+h).length == 1 ? "0"+h : h;
	m = (""+m).length == 1 ? "0"+m : m;
	s = (""+s).length == 1 ? "0"+s : s;
	return year+"-"+month+"-"+day +" "+ h+":"+m+":"+s;
};
